package game;

import java.io.File;
import java.io.IOException;

import system.*;

public class Boss extends Actor{

	AnimatedSprite[] sprites = new AnimatedSprite[5];
	Scroller theScene;
	Director sceneBulletDirector;

	int state = 1;
	int counter = 0;
	int ydir = 1;

	final int STATE_DEAD = -1;
	final int STATE_DEFAULT = 0;
	final int STATE_INTRO = 1;
	final int STATE_ATTACK = 2;
	final int STATE_MOVING = 3;
	final int STATE_DYING = 4;

	public Boss(Scroller scene, Director bulletDirector, AnimatedSprite sprite, int power) {
		super("Boss", sprite, 480, 50, -5, 0, power, -1);
		this.theScene = scene;
		this.sceneBulletDirector = bulletDirector;

		state = STATE_INTRO;
		myHp = power * 100 + 100;

		try {
			sprites[0] = new AnimatedSprite(new File("assets/sprites/BOSSSTAND.png"),108,121,true, 90);
			sprites[1] = new AnimatedSprite(new File("assets/sprites/BOSSMOVE.png"),108,121,true, 90);
			sprites[2] = new AnimatedSprite(new File("assets/sprites/BOSSCHARGE.png"),108,121,true, -1);
			sprites[3] = new AnimatedSprite(new File("assets/sprites/BOSSMOVE.png"),108,121,true, 90);
			sprites[4] = new AnimatedSprite(new File("assets/sprites/BOSSCHARGE.png"),108,121,true, -1);
		} catch (IOException e) {
			System.out.println("We couldn't load player sprites");
		}

		sprite = sprites[STATE_INTRO];

	}

	@Override
	public void update(){
		sprite.update();

		x += xvel;
		y += yvel;

		if(y <= 0 || y >= 179){
			ydir = -ydir;
		}

		counter++;

		switch(state){
		case STATE_INTRO: {
			if(counter >= 20){
				xvel = 0;
				state = STATE_DEFAULT;
				sprite = sprites[0];
				counter = 0;
			}
			break;
		}
		case STATE_DEFAULT:{
			sprite = sprites[0];
			if(getHp() <= 0){
				state = -1;
				sprite = sprites[4];
			}
			else{
				int action = (int)(Math.random() * 100);
				if (action <= 5){ // Special Attack
					state = STATE_ATTACK;
					sprite = sprites[2];
					counter = 0;
				}
				else if (action <= 30){
					ydir = (Math.random() > 0.5)? -1 : 1; 
					if (getY() <= 50){
						ydir = 1;
					}
					if (getY() >= 129){
						ydir = -1;
					}

					state = STATE_MOVING;
					sprite = sprites[3];
					counter = 0;
				}
				else if (action <= 40){ // Regular Attack
					sprite = sprites[2];
					sprite.setFrameIndex(14);
					sceneBulletDirector.addEnemyBullet(this, -5F, 0F);
					counter = 0;
				}

				else if (action <= 50){ // Regular Triple Attack
					sprite = sprites[2];
					sprite.setFrameIndex(14);
					sceneBulletDirector.addEnemyBullet(this, -5F, -0.25F);
					sceneBulletDirector.addEnemyBullet(this, -5F, 0F);
					sceneBulletDirector.addEnemyBullet(this, -5F, 0.25F);
					counter = 0;
				}
			}
			break;
		}

		case STATE_MOVING: {
			yvel = ydir*5;
			if(counter >= 10){
				yvel = 0;
				state = STATE_DEFAULT;
				counter = 0;
			}
			break;
		}
		case STATE_ATTACK: {

			if(counter < 13){
				sprite.setFrameIndex(counter);
			}
			else if(counter >= 33 && counter < 35){
				sprite.setFrameIndex(counter-20);
			}

			float playerX = theScene.getPlayerX();
			float playerY = theScene.getPlayerY();

			float diffx = getX() - playerX;
			float diffy = getY() - playerY + 10;

			double angle = Math.atan2(diffy, diffx);

			float dx = -(float)(Math.cos(angle) * 5);
			float dy = -(float)(Math.sin(angle) * 5);

			if(counter > 12){
				sceneBulletDirector.addEnemyBullet(this, dx, dy);

			}
			if(counter >= 35 ){
				yvel = 0;
				state = STATE_DEFAULT;
				counter = 0;
			}
			break;
		}
		case STATE_DEAD: {
			if (counter < 20 && counter % 5 == 0){
				sceneBulletDirector.addEnemyBullet(this, -5F, -0.8F);
				sceneBulletDirector.addEnemyBullet(this, -5F, -0.6F);
				sceneBulletDirector.addEnemyBullet(this, -5F, -0.4F);
				sceneBulletDirector.addEnemyBullet(this, -5F, -0.2F);
				sceneBulletDirector.addEnemyBullet(this, -5F, 0F);
				sceneBulletDirector.addEnemyBullet(this, -5F, 0.2F);
				sceneBulletDirector.addEnemyBullet(this, -5F, 0.4F);
				sceneBulletDirector.addEnemyBullet(this, -5F, 0.6F);
				sceneBulletDirector.addEnemyBullet(this, -5F, 0.8F);
			}
			if (counter>= 20){
				this.x = 500;
				this.y = 500;
				theScene.bosskilled = true;
			}
			break;

		}

		}

	}

}
